const express=require("express")
const mongodb=require("mongodb")
const ObjectId=mongodb.ObjectId
const path=require("path")
const bodyParser=require("body-parser")
const app=express()
const {getStoreDB}=require("./mongo")
const Const=require("./const_value")
const jwt=require("jsonwebtoken")
const cookieParser=require("cookie-parser")
const key="hnhgbhghdbdjsuhdgts"
const whitelist=["/login","/register"]

app.use(bodyParser.json());
app.use(cookieParser())
app.use(express.static(path.join(__dirname,"..","public")))
app.use('/user', userRouter);
app.use('/products', productRouter);
app.use(function(req,res,next){
    console.log(whitelist,req.url)
  if(whitelist.indexOf(req.url)!==-1){
    next()
  }
  else{
    jwt.verify(req.cookies.token,key,function(err,login){
        if(err){
            res.send({
                status:11110,
                msg:"token失效 请重新登录"
            })
            return
        }
        next()
       })
  }
})
app.post("/register",function(req,res){
    getStoreDB(function(storeDB){
        storeDB.collection("userinfo").insert(req.body,function(err,result){
         if(err){
             res.send({
                status: 10001,
                msg: err.message
             })
             return
         }
         res.send({
            status: Const.SUCCESSFUL,
            msg: 'ok',
            data: {
                user_id: result.insertedId
            }
         })
        })
    })
})

app.post("/login",function(req,res){
    getStoreDB(function(storeDB){
        storeDB.collection("userinfo").find({login_name:req.body.login_name,password:req.body.password}).toArray(function(err,result){
         if(err){
             res.send({
                status: 10001,
                msg: err.message
             })
             return
         }
         const token=jwt.sign({login_name:req.body.login_name},key)
         res.cookie("token",token)
         if(result.length!==0){
            res.send({
                status: Const.SUCCESSFUL,
                msg: 'ok',
                data: {
                    user_id: result.insertedId
                }
             }) 
         }
        })
    })
})

app.get("/get_product",function(req,res){
    getStoreDB(function(storeDB){
        storeDB.collection("goods").find({}).toArray(function(err,result){
         if(err){
             res.send({
                status: 10002,
                msg: err.message
             })
             return
         }
            res.send({
                status: Const.SUCCESSFUL,
                msg: 'ok',
                data: {
                    result:result
                }
             }) 
       
        })
    })
})

app.post("/del_product",function(req,res){
    getStoreDB(function(storeDB){
        console.log(ObjectId(req.body._id))
        storeDB.collection("goods").remove({_id:ObjectId(req.body._id)},function(err,result){
         if(err){
             res.send({
                status: 10002,
                msg: err.message
             })
             return
         }
            res.send({
                status: Const.SUCCESSFUL,
                msg: 'ok',
                data: {
                    result:result
                }
             }) 
        })
    })
})

app.post("/add_product",function(req,res){
    getStoreDB(function(storeDB){
        storeDB.collection("goods").insert(req.body,function(err,result){
         if(err){
             res.send({
                status: 10002,
                msg: err.message
             })
             return
         }
            res.send({
                status: Const.SUCCESSFUL,
                msg: 'ok',
                data: {
                    result:result
                }
             }) 
        })
    })
})

app.post("/revise_product",function(req,res){
    getStoreDB(function(storeDB){
        var _id=req.body._id
        var {goods_name,price,number,dw}=req.body
        console.log(_id)
        storeDB.collection("goods").updateOne({_id:ObjectId(_id)},{$set:{goods_name,price,number,dw}},function(err,result){
         if(err){
             res.send({
                status: 10002,
                msg: err.message
             })
             return
         }
            res.send({
                status: Const.SUCCESSFUL,
                msg: 'ok',
                data: {
                    result:result
                }
             }) 
        })
    })
})

app.listen(3000, function (err) {
   if (err) {
       console.log(err);
       return;
   }
   console.log("服务器开启了")
});